define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'orders.orders/index',
                    add_url: '',
                    edit_url: '',
                    del_url: 'orders.orders/del',
                    multi_url: '',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'id', title: 'ID',operate:false},
                        {field: 'username', title: '收货姓名',operate:false},
                        {field: 'mobile', title: '收货电话',operate:false},
                        {field: 'address', title: '收货地址',operate:false},
                        {field: 'order_sn', title: '订单编号', operate:'LIKE'},
                        {field: 'goods_price', title: '金额',operate:false},
                        {field: 'payment_price', title: '支付金额',operate:false},
                        {field: 'number', title: '数量',operate:false},
                        {field: 'status', title: '支付状态', searchList: {1: '待支付', 2: '已付款', 3: '已发货', 4: '已签收', 5: '已评价',9: '已取消'} , formatter:function (value) {
                                if(value == 1){
                                    return '<span class="text-success"><i class="fa fa-circle"></i> 待支付</span>';
                                }else if(value == 2){
                                    return '<span class="text-success"><i class="fa fa-circle"></i> 已付款</span>';
                                }else if(value == 3){
                                    return '<span class="text-success"><i class="fa fa-circle"></i> 已发货</span>';
                                }else if(value == 4){
                                    return '<span class="text-success"><i class="fa fa-circle"></i> 已签收</span>';
                                }else if(value == 5){
                                    return '<span class="text-success"><i class="fa fa-circle"></i> 已评价</span>';
                                }else if(value == 9){
                                    return '<span class="text-success"><i class="fa fa-circle"></i> 已取消</span>';
                                }
                            }
                        },
                        {field: 'payment_method', title: __('支付类型'), searchList: {"wechat":__('微信支付'),"balance":__('余额支付')}, formatter:function (value) {
                                if(value == 'wechat'){
                                    return '<span class="text-success"><i class="fa fa-circle"></i> 微信支付</span>';
                                }else if(value == 'balance'){
                                    return '<span class="text-success"><i class="fa fa-circle"></i> 余额支付</span>';
                                }
                            }
                         },
                        {field: 'ptype', title: __('产品类型'), searchList: {1:'书籍',2:'产品'}, formatter:function (value) {
                                if(value == 1){
                                    return '<span class="text-success"><i class="fa fa-circle"></i> 书籍</span>';
                                }else if(value == 2){
                                    return '<span class="text-success"><i class="fa fa-circle"></i> 产品</span>';
                                }
                            }
                        },
                        {field: 'createtime', title: '创建时间', formatter: Table.api.formatter.datetime,
                            operate: 'BETWEEN', addclass: 'datetimepicker', data: 'data-date-format="YYYY-MM-DD"'},
                        {field: 'paytime', title: '支付时间', formatter: Table.api.formatter.datetime,operate:false},
                        {field: 'sendtime', title: '发货时间', formatter: Table.api.formatter.datetime,operate:false},
                        // {field: 'operate', title: __('Operate'), events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            // 绑定TAB事件
            $('.panel-heading a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var field = $(this).closest("ul").data("field");
                var value = $(this).data("value");
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    var filter = {};
                    if (value !== '') {
                        filter[field] = value;
                    }

                    params.filter = JSON.stringify(filter);
                    return params;
                };

                table.bootstrapTable('refresh', {});
                return false;
            });
            //*************************** 自定义export开始
            var submitForm = function (ids, layero) {
                var options = table.bootstrapTable('getOptions');
                console.log(options);
                var columns = [];
                $.each(options.columns[0], function (i, j) {
                    if (j.field && !j.checkbox && j.visible && j.field != 'operate') {
                        columns.push(j.field);
                    }
                });
                var search = options.queryParams({});
                $("input[name=search]", layero).val(options.searchText);
                $("input[name=ids]", layero).val(ids);
                $("input[name=filter]", layero).val(search.filter);
                $("input[name=op]", layero).val(search.op);
                $("input[name=columns]", layero).val(columns.join(','));
                $("#formid", layero).submit();
            };
            $(document).on("click", ".btn-export", function () {
                var ids = Table.api.selectedids(table);
                var page = table.bootstrapTable('getData');
                var all = table.bootstrapTable('getOptions').totalRows;
                // console.log(ids, page, all);
                Layer.confirm("请选择导出的选项<form id='formid' action='" + Fast.api.fixurl("orders.orders/export") + "' method='post' target='_blank'>" +
                    "<input type='hidden' name='ids'  />" +
                    "<input type='hidden' name='filter' >" +
                    "<input type='hidden' name='op'>" +
                    "<input type='hidden' name='search'>" +
                    "<input type='hidden' name='columns'></form>", {
                    title: '导出数据',
                    btn: ["选中项(" + ids.length + "条)", "本页(" + page.length + "条)", "全部(" + all + "条)"],
                    // btn: ["选中项(" + ids.length + "条)"],
                    success: function (layero, index) {
                        $(".layui-layer-btn a", layero).addClass("layui-layer-btn0");
                    },
                    yes: function (index, layero) {
                        submitForm(ids.join(","), layero);
                        return false;
                    },
                    btn2: function (index, layero) {
                        var ids = [];
                        $.each(page, function (i, j) {
                            ids.push(j.id);
                        });
                        submitForm(ids.join(","), layero);
                        // submitForm(search, layero);
                        return false;
                    },
                    btn3: function (index, layero) {
                        submitForm("all", layero);
                        return false;
                    }
                });
            });
            //*************************** 自定义export结束

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

    };
    return Controller;
});

